package com.sap.sap_mangers.controller;

import com.sap.sap_mangers.Vo.UserVo.UserMessage;
import com.sap.sap_mangers.dto.Navigation.AddNavigation;
import com.sap.sap_mangers.dto.Navigation.UpdateNavigation;
import com.sap.sap_mangers.service.NavigationService;
import com.sap.sap_mangers.service.UserService;
import com.sap.sap_mangers.until.ConstantUntil;
import com.sap.sap_mangers.until.JwtUtil;
import com.sap.sap_mangers.until.SimpleJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 樊东升
 * @date 2022/8/19 23:14
 */
@RestController
@RequestMapping("/Site")
public class NavigationController {
    @Autowired
    private NavigationService navigationService;
    @Autowired
    private UserService userService;

    private static Integer getInteger(MultipartHttpServletRequest params, String param) {
        Integer parameter;
        String parameterString = params.getParameter(param);
        if (parameterString == null || parameterString.isEmpty()) {
            parameter = null;
        } else {
            parameter = Integer.valueOf(parameterString);
        }
        return parameter;
    }

    /**
     * 获取导航信息
     *
     * @return
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getSapNavigation")
    public SimpleJson getNavigation() {
        return new SimpleJson(navigationService.getNavigation());
    }

    @PreAuthorize("hasAuthority('旧管理员')")
    @PostMapping("/addSapNavigation")
    public SimpleJson addSapNavigation(HttpServletRequest request) {
        MultipartHttpServletRequest params = ((MultipartHttpServletRequest) request);
        List<MultipartFile> image = ((MultipartHttpServletRequest) request).getFiles("image");
        if (image.size() != 1) {
            return new SimpleJson(1, "图片数量不符合要求");
        }
        String url = params.getParameter("url");
        String urlName = params.getParameter("urlName");
        if (url == null || urlName == null || url.isEmpty() || urlName.isEmpty()) {
            return new SimpleJson(1, "链接和名字不可以为空");
        }
        String urlNote = params.getParameter("note");
        if (urlNote.isEmpty()) {
            urlNote = null;
        }
        int classify;
        try {
            classify = Integer.parseInt(params.getParameter("classify"));
        } catch (Exception e) {
            return new SimpleJson(1, "分类的类型错误");
        }
        AddNavigation addNavigation = new AddNavigation(url, urlName, image.get(0), urlNote, classify);
        return navigationService.addSapNavigation(addNavigation);
    }

    @PreAuthorize("hasAuthority('旧管理员')")
    @PostMapping("/updateSapNavigation")
    public SimpleJson updateSapNavigation(HttpServletRequest request) {
        MultipartHttpServletRequest params = ((MultipartHttpServletRequest) request);
        List<MultipartFile> image = ((MultipartHttpServletRequest) request).getFiles("image");
        try {
            int id;
            try {
                id = Integer.parseInt(params.getParameter("id"));
            } catch (Exception e) {
                return new SimpleJson(1, "id不可以为空");
            }
            Integer classify = getInteger(params, "classify");
            Integer rank = getInteger(params, "rank");
            String url = params.getParameter("url");
            if (url.isEmpty()) {
                url = null;
            }
            String urlName = params.getParameter("urlName");
            if (urlName.isEmpty()) {
                urlName = null;
            }
            String urlNote = params.getParameter("note");
            if (urlNote.isEmpty()) {
                urlNote = null;
            }
            UpdateNavigation updateNavigation;
            if (image.size() == 0) {
                updateNavigation = new UpdateNavigation(id, url, urlName, null, rank, classify, urlNote);
            } else {
                updateNavigation = new UpdateNavigation(id, url, urlName, image.get(0), rank, classify, urlNote);
            }
            return navigationService.updateNavigation(updateNavigation);
        } catch (NumberFormatException e) {
            return new SimpleJson(1, "参数格式错误");
        }
    }

    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getSapNavigationClassify")
    public SimpleJson getSapNavigationClassify() {
        return new SimpleJson(navigationService.getSapNavigationClassify());
    }

    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getRank")
    public SimpleJson getRank(@RequestParam("classify") Integer classify) {
        return new SimpleJson(navigationService.getRank(classify));
    }

    @PreAuthorize("hasAuthority('旧管理员')")
    @GetMapping("/deleteSite")
    public SimpleJson deleteSite(@RequestParam("id") String id) {
        UserMessage userMessage = userService.getUserMessage(JwtUtil.getUserForToken());
        if (userMessage.getRole() > ConstantUntil.ROLE_PROPAGANDA_DEPUTY_MINISTER) {
            return new SimpleJson(1, "权限不足，无法删除");
        }
        int intId;
        try {
            intId = Integer.parseInt(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new SimpleJson(1, "id转换失败，可能是传入了非数值");
        }
        navigationService.deleteMessageById(intId);
        return SimpleJson.SUCCESS;
    }

    /**
     * 获取用于用户端展示的站点
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getSiteForUser")
    public SimpleJson getSiteForUser() {
        return new SimpleJson(navigationService.getNavigationForUser());
    }


}
